$(function (){

	/**
	 * Created by zxk on 16/3/14.
	 */

	var sharedUserToken = $("#openId").val();
	var createdTime = $("#createdTime").val();

    wx.config({
        debug: false,
        appId: $("#appid").val(),
        timestamp: $("#timestamp").val(),
        nonceStr: $("#nonceStr").val(),
        signature: $("#signature").val(),
        jsApiList: [
            // 所有要调用的 API 都要加到这个列表中
            'getLocation'
        ]
    });

	wx.ready(function(){
	    wx.getLocation({
	        type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
	        success: function (res) {
	            var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
	            var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
	            var center = new qq.maps.LatLng(latitude,longitude);
	            map = new qq.maps.Map(document.getElementById("map-container"), {
	                center: center,
	                zoom:15
	            });
	            //var anchor = new qq.maps.Point(0, 39),
	            //    size = new qq.maps.Size(42, 42),
	            //    origin = new qq.maps.Point(0, 0),
	            //    icon = new qq.maps.MarkerImage(
	            //        "../../../static/img/pick_1.png", null,null,null,new qq.maps.Size(100, 59)
	            //    );
	            marker = new qq.maps.Marker({
	                position: center,
	                map: map,
	                visible: false,
	                disableDefaultUI: true
	            });
	            //marker.setIcon(icon);
	            setAddress(center);

	            qq.maps.event.addListener(map, 'center_changed', function() {
	                marker.setPosition(map.getCenter());
	                setAddress(map.getCenter());
	            });

	            $(".point").on('click',function(){
	                var lat=marker.getPosition().getLat();
	                var lng=marker.getPosition().getLng();
	                var address=$("#name").text();
	                if(lat && lng){
	                    $.ajax({
	                        type: "POST",
	                        async: true,
	                        data: {
	                        	"lat":lat,
	                        	"lng":lng,
	                        	"address":address
	                        	},
	                        dataType: "json",
	                        url: $("#url2").val()+"/share/send?openId="+sharedUserToken,
	                        success: function(result){
	                            if(result.code == 200){
	                                alertSuccess("发送成功",address);
	                            }else{
	                                alertWarn(false, "发送失败,请朋友再发个分享地址吧");
	                            }
	                        }
	                    })
	                }
	            })
	        },
	        error: function(){
	            alertWarn("警告", "请打开微信访问位置信息的设置");
	        }
	    });
	})

	function setAddress(point) {
	    geocoder = new qq.maps.Geocoder({
	        complete: function (result) {
	            $(".address").show();
	            var poi = result.detail.nearPois[0];
	            var curName = poi.name;
	            var curAddress=poi.address;
	            var curLatLng=result.detail.location;
	            $("#name").text(curName);
	            $("#address").text(curAddress);
	        },
	        error: function(){
	            alertWarn(false,"获取对应位置信息失败");
	        }
	    });

	    geocoder.getAddress(point);
	}
	function alertSuccess(title, content){
	    var jc = $.confirm({
	        title: title,
	        icon: 'glyphicon glyphicon-ok',
	        content: content,
	        //confirmButtonClass: "btn-success",
	        confirmButton: '确定',
	        cancelButton: false
	    })
	    jc.$content.addClass("text-center");
	    jc.$content.css({"color":"#9A3036","font-size":"16px"});
	};
	function alertWarn(title, content){
	    var jc = $.confirm({
	        title: title,
	        icon: 'glyphicon glyphicon-warning-sign',
	        content: content,
	        //confirmButtonClass: "btn-success",
	        confirmButton: '关闭',
	        cancelButton: false
	    })
	    jc.$content.addClass("text-center");
	    jc.$content.css({"color":"#9A3036","font-size":"16px"});
	};
	function alertWarn(title, content){
	    var jc = $.confirm({
	        title: title,
	        icon: 'glyphicon glyphicon-warning-sign',
	        content: content,
	        //confirmButtonClass: "btn-success",
	        confirmButton: '关闭',
	        cancelButton: false
	    })
	    jc.$content.addClass("text-center");
	    jc.$content.css({"color":"#9A3036","font-size":"16px"});
	};


	if((new Date()).getTime() - createdTime < 30*60*60*1000){
	}else{
	    $("h4").show();
	}


})